import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
// 按需导入
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
// @
import path from 'path'
// svg
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
// mock
import { UserConfigExport, ConfigEnv } from 'vite'
import { viteMockServe } from 'vite-plugin-mock'
// https://vitejs.dev/config/
export default defineConfig(({command, mode})=>{
  // 获取各种环境下的对应的变量
  let env = loadEnv(mode, process.cwd());
  // console.log('env',env)
  return {
    base: env.VITE_USER_NODE_ENV === 'production' ? './' : '/',
    plugins: [
      vue(),
      viteMockServe({
        localEnabled: command === 'serve', // 开发阶段使用mock接口
      }),
      createSvgIconsPlugin({
        // path: fill='currentColor'
        iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
        symbolId: 'icon-[dir]-[name]',
      }),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
      
    ],
    resolve: {
      alias: {
        '@': path.resolve('./src'),
      },
    },
    // scss 全局变量配置
    css: {
      preprocessorOptions: {
        scss: {
          javascriptEnabled: true,
          additionalData: '@import "./src/styles/variable.scss";',
        },
      },
    },
    // 分包
    build: {
      rollupOptions: {
        manualChunks(id) {
          if (id.includes("node_modules")) {
            return "vendor";
          }
        },
      },
    },
    //代理跨域
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        }
      }
    }
  }
})